<?php

/**
 * @see IMailHandler
 */
require_once 'IMailHandler.php';

class SmtpMailHandler implements IMailHandler
{
    const NEWLINE = "\r\n";

    /**
     * The SMTP host.
     *
     * @var string
     */
    protected $host;

    /**
     * The password.
     *
     * @var string
     */
    protected $password;

    /**
     * The SMTP host port.
     *
     * @var integer
     */
    protected $port;

    /**
     * The timeout for the SMTP connection.
     *
     * @var integer
     */
    protected $timeout;

    /**
     * The username.
     *
     * @var string
     */
    protected $username;

    /**
     * Constructor.
     *
     * @param string $username
     * @param string $password
     * @param string $host
     * @param integer $port
     * @param integer $timeout
     * @return SmtpMailHandler
     */
    public function __construct(
        $username = null,
        $password = null,
        $host = 'localhost',
        $port = 25,
        $timeout = 30
        )
    {
        $this->username = $username;
        $this->password = $password;
        $this->host = $host;
        $this->port = (int) $port;
        $this->timeout = (int) $timeout;
        return $this;
    }

    /**
     * Sends the mail.
     *
     * @param Mail $mail
     * @return SmtpMailHandler
     */
    public function sendMail(Mail $mail)
    {
        $smtpConnection = fsockopen(
            $this->host,
            $this->port,
            $errorNo,
            $errorString,
            $this->timeout
        );
        
        $smtpResponse = fgets($smtpConnection, 515);

        if (!$smtpConnection) {
            throw new Exception('Could not connect to SMTP host: ' . $smtpResponse);
        }

        $logArray = array();

        // Say hello
        fputs($smtpConnection, 'HELO ' . $this->host . self::NEWLINE);
        $smtpResponse = fgets($smtpConnection, 515);
        $logArray['heloresponse'] = "$smtpResponse";

//        // STARTTLS
//        if ($this->starttls) {
//        fputs($smtpConnection, 'STARTTLS ' . self::NEWLINE);
//        $smtpResponse = fgets($smtpConnection, 515);
//        $logArray['starttls'] = "$smtpResponse";


        if ($this->username != null) {

            // Request authorization
            fputs($smtpConnection, "AUTH LOGIN" . self::NEWLINE);
            $smtpResponse = fgets($smtpConnection, 515);
            $logArray['authrequest'] = "$smtpResponse";

            // Send username
            fputs(
                $smtpConnection,
                base64_encode($this->username) . self::NEWLINE
            );
            $smtpResponse = fgets($smtpConnection, 515);
            $logArray['authusername'] = "$smtpResponse";

            // Send password
            fputs(
                $smtpConnection,
                base64_encode($this->password) . self::NEWLINE
            );
            $smtpResponse = fgets($smtpConnection, 515);
            $logArray['authpassword'] = "$smtpResponse";
        }

        // Email From
        fputs($smtpConnection, 'MAIL FROM: ' . $mail->getFrom() . self::NEWLINE);
        $smtpResponse = fgets($smtpConnection, 515);
        $logArray['mailfromresponse'] = "$smtpResponse";

        // Email To
        fputs($smtpConnection, 'RCPT TO: ' . $mail->getTo() . self::NEWLINE);
        $smtpResponse = fgets($smtpConnection, 515);
        $logArray['mailtoresponse'] = "$smtpResponse";

        //The Email
        fputs($smtpConnection, 'DATA' . self::NEWLINE);
        $smtpResponse = fgets($smtpConnection, 515);
        $logArray['data1response'] = "$smtpResponse";

        fputs($smtpConnection, $mail->getHeaders());
        $smtpResponse = fgets($smtpConnection, 515);
        $logArray['data2response'] = "$smtpResponse";

        // Say good bye
        fputs($smtpConnection, 'QUIT' . self::NEWLINE);
        $smtpResponse = fgets($smtpConnection, 515);
        $logArray['quitresponse'] = "$smtpResponse";

        print_r($logArray);

        fclose($smtpConnection);

        return $this;
    }
}